package com.nervenets.template.controller.user.params;

import com.nervenets.general.web.params.EntityPagingParams;
import com.nervenets.template.hibernate.domain.user.Manager;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import lombok.experimental.SuperBuilder;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.List;

@EqualsAndHashCode(callSuper = true)
@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
@SuperBuilder
public class ManagerPagingParams extends EntityPagingParams<Manager> {
    @ApiModelProperty(value = "权限组ID，不填不筛选")
    private long roleGroupId;
    @ApiModelProperty(value = "关键词，不填不筛选")
    private String keyword;
    @ApiModelProperty("是否启用：0所有，1启用，2禁用")
    private int enable;

    @Override
    public void generateSpecification(List<Predicate> predicates, Root<Manager> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
        if (getRoleGroupId() > 0) {
            addEqualsPredicate(root.join("roleGroups").get("id"), roleGroupId, true);
        }
        addOrLikePredicate(keyword, root.get("name"), root.get("mobile"));
        addYesOrNoPredicate(root.get("enable"), enable, 1, 2);
    }
}
